﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using PeapodCatcher.Properties;

namespace PeapodCatcher
{
    public static class Log
    {
        public static void LogError(string location, Exception ex) {
            if (Settings.Default.EnableLogging)
            {
                WriteToLogFile(DateTime.Now + " " + location + ": " + ex.ToString());
                Console.WriteLine(location + ": " + ex.ToString());
            }
        }

        public static void LogMessage(string location, string message)
        {
            if (Settings.Default.EnableLogging)
            {
                WriteToLogFile(message);
                Console.WriteLine(message);
            }
        }

        private static void WriteToLogFile(string msg)
        {
            string appDataDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "PeapodCatcher");
            DirectoryInfo di = new DirectoryInfo(appDataDir);
            if (!di.Exists)
            {
                di.Create();
            }

            string logFile = Path.Combine(appDataDir,
                    DateTime.Today.Year + "-" + DateTime.Today.Month + "-" + DateTime.Today.Day + "-Log.txt");
            using (TextWriter tw = new StreamWriter(logFile, true))
            {
                tw.WriteLine(msg);
                tw.Close();
            }
        }
    }
}
